
#include "front/text_normalize.h"

using namespace std;
using namespace ppspeech;

const wregex mcWregexZhNoNumber(L"No.\\s*\\d+");
const wregex mcWregexEnNoNumber(L" No.\\s*\\d+(-\\d+)?");

void TextNormalizer::TnReNo2Number(wstring* fpInWsentence, const LanguageEnum fcInLanguage)
{
	wsmatch tWsmatch;

	while( regex_search(*fpInWsentence, tWsmatch, (fcInLanguage==LANG_EN?mcWregexEnNoNumber:mcWregexZhNoNumber) ) )
	{
		string tStrNoNumber = wstring2utf8string(tWsmatch[0]);		//匹配到的字符串
		string tStrNumber = "";
		for(const char tcChar : tStrNoNumber) {
			if(tcChar >= '0'  &&  tcChar <= '9' || tcChar == '-') {
				tStrNumber += tcChar;
			}
		}
		vector<string> tVecNumber = StrSplit(tStrNumber, "-");

		string tStrReplace = " number " + ReplaceMethod(vector<string>{tVecNumber[0]}, RME_en_negative_decimal) + " ";

		for(size_t i=1; i<tVecNumber.size(); i++) {
			tStrReplace += "to ";
			tStrReplace += ReplaceMethod(vector<string>{tVecNumber[i]}, RME_en_negative_decimal) + " ";
		}

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), utf8string2wstring(tStrReplace));
	}
}


const wregex mcWregexEnAtNumber(L" (at|from) \\d+");

void TextNormalizer::TnReEnAtNumber(wstring* fpInWsentence, const LanguageEnum fcInLanguage)
{
	if(fcInLanguage==LANG_ZH) {
		return;
	}

	wsmatch tWsmatch;

	while( regex_search(*fpInWsentence, tWsmatch, mcWregexEnAtNumber) )
	{
		string tStrAtNumber = wstring2utf8string(tWsmatch[0]);		//匹配到的字符串
		string tStrPrefix = "";
		string tStrNumber = "";

		for(const char tcChar : tStrAtNumber) {
			if(tcChar >= '0' && tcChar <= '9') {
				break;
			}
			tStrPrefix += tcChar;
		}

		for(const char tcChar : tStrAtNumber) {
			if(tcChar >= '0' && tcChar <= '9') {
				tStrNumber += tcChar;
			}
		}

		string tStrReplace = tStrPrefix + "  ";

		if(tStrNumber.length() == 4) {	//比如，“at 1010”读成“at ten ten”
			tStrReplace += ReplaceMethod(vector<string>{tStrNumber}, RME_en_year) + " ";
		}
		else {
			tStrReplace += tStrNumber;
		}

		WstrReplacePosition(fpInWsentence, tWsmatch.position(0), tWsmatch.length(0), utf8string2wstring(tStrReplace));
	}
}
